<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <title>房源信息查看</title>
    <link rel="stylesheet" href="zhuye.css">
</head>

<body class="bg-gray-100 font-sans">
<div id="app" class="container mx-auto p-8">
    <h1 class="text-3xl font-bold mb-6 text-center">房源信息查看</h1>
    <div class="flex justify-between mb-4">
        <button @click="fetchAllHouses" class="bg-blue-500 text-white py-2 px-4 rounded hover:bg-blue-600 focus:outline-none focus:ring focus:border-blue-300">全部房源</button>
        <button @click="fetchLatestHouses" class="bg-green-500 text-white py-2 px-4 rounded hover:bg-green-600 focus:outline-none focus:ring focus:border-green-300">最新房源</button>
        <button @click="fetchCheapestHouses" class="bg-yellow-500 text-white py-2 px-4 rounded hover:bg-yellow-600 focus:outline-none focus:ring focus:border-yellow-300">最便宜房源</button>
    </div>
    <div class="mb-4">
        <label for="searchTitle" class="block text-gray-700 font-bold mb-2">按标题搜索</label>
        <input type="text" v-model="searchTitle" id="searchTitle" name="searchTitle" class="w-full text-black border border-gray-300 p-2 rounded focus:outline-none focus:border-blue-500">
        <button @click="searchHouses" class="bg-blue-500 text-white py-2 px-4 rounded hover:bg-blue-600 focus:outline-none focus:ring focus:border-blue-300 mt-2">搜索</button>
    </div>
    <table class="w-full border-collapse border border-gray-300">
        <thead>
        <tr>
            <th class="border border-gray-300 p-2">房产编号</th>
            <th class="border border-gray-300 p-2">房源标题</th>
            <th class="border border-gray-300 p-2">房屋类型</th>
            <th class="border border-gray-300 p-2">户型</th>
            <th class="border border-gray-300 p-2">所在层</th>
            <th class="border border-gray-300 p-2">总层数</th>
            <th class="border border-gray-300 p-2">建筑面积</th>
            <th class="border border-gray-300 p-2">使用面积</th>
            <th class="border border-gray-300 p-2">省份</th>
            <th class="border border-gray-300 p-2">城市</th>
            <th class="border border-gray-300 p-2">区县</th>
            <th class="border border-gray-300 p-2">详细地址</th>
            <th class="border border-gray-300 p-2">发布时间</th>
            <th class="border border-gray-300 p-2">联系电话</th>
            <th class="border border-gray-300 p-2">月租金</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="house in houses" :key="house.fangwuID">
            <td class="border border-gray-300 p-2">{{ house.fangchanbianhao }}</td>
            <td class="border border-gray-300 p-2">{{ house.fangyuanbiaoti }}</td>
            <td class="border border-gray-300 p-2">{{ house.fangwuleixing }}</td>
            <td class="border border-gray-300 p-2">{{ house.huxing }}</td>
            <td class="border border-gray-300 p-2">{{ house.suozaiceng }}</td>
            <td class="border border-gray-300 p-2">{{ house.zongcengshu }}</td>
            <td class="border border-gray-300 p-2">{{ house.jianzhumianji }}</td>
            <td class="border border-gray-300 p-2">{{ house.shiyongmianji }}</td>
            <td class="border border-gray-300 p-2">{{ house.shengfen }}</td>
            <td class="border border-gray-300 p-2">{{ house.chengshi }}</td>
            <td class="border border-gray-300 p-2">{{ house.quxian }}</td>
            <td class="border border-gray-300 p-2">{{ house.xiangxidizhi }}</td>
            <td class="border border-gray-300 p-2">{{ house.fabushijian }}</td>
            <td class="border border-gray-300 p-2">{{ house.lianxidianhua }}</td>
            <td class="border border-gray-300 p-2">{{ house.yuezujin }}</td>
        </tr>
        </tbody>
    </table>
</div>

<script>
    const { createApp } = Vue;

    createApp({
        data() {
            return {
                houses: [],
                searchTitle: ''
            };
        },
        methods: {
            fetchAllHouses() {
                fetch('http://localhost:8888/fangwujibenxinxi/fangyuan')
                    .then(response => response.json())
                    .then(data => {
                        this.houses = data.data;
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        alert('获取全部房源信息时出错');
                    });
            },
            fetchLatestHouses() {
                fetch('http://localhost:8888/fangwujibenxinxi/zuixinfangyuan')
                    .then(response => response.json())
                    .then(data => {
                        this.houses = data.data;
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        alert('获取最新房源信息时出错');
                    });
            },
            fetchCheapestHouses() {
                fetch('http://localhost:8888/fangwujibenxinxi/zuipianyifangyuan')
                    .then(response => response.json())
                    .then(data => {
                        this.houses = data.data;
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        alert('获取最便宜房源信息时出错');
                    });
            },
            searchHouses() {
                fetch(`http://localhost:8888/fangwujibenxinxi/chakan?fangyuanbiaoti=${this.searchTitle}`)
                    .then(response => response.json())
                    .then(data => {
                        this.houses = data.data;
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        alert('搜索房源信息时出错');
                    });
            }
        },
        mounted() {
            this.fetchAllHouses();
        }
    }).mount('#app');
</script>
</body>

</html>
